/*
 * Copyright (c) 2019. Mikes. All rights reserved.
 *
 * You can contact me with sending email to bWlrZXNfMjAxMkAxNjMuY29t for more information!
 */
package com.climber.android.commonsdk.app.configuration

import android.app.Application
import android.content.Context
import android.os.Build
import android.webkit.WebView
import androidx.multidex.MultiDex
import cat.ereza.customactivityoncrash.CustomActivityOnCrash
import com.alibaba.android.arouter.launcher.ARouter
import com.climber.android.commonsdk.app.AppConstants
import com.climber.android.commonsdk.app.AppEnv
import com.climber.android.commonsdk.event.BusProvider
import com.climber.android.commonsdk.event.CommonBusEvent
import com.umeng.analytics.MobclickAgent
import com.umeng.commonsdk.UMConfigure
import io.ganguo.library.klog.KLog
import io.ganguo.library.mvp.ui.mvp.base.delegate.AppLifecycles
import io.ganguo.library.mvp.util.AppUtils
import io.ganguo.library.mvp.util.CBaseUtils
import io.library.android.loading.Gloading
import io.library.android.net.NetworkManager
import io.library.android.zxing.ZXingLibrary
import io.realm.Realm

/**
 * ================================================
 * 展示 [AppLifecycles] 的用法
 * ================================================
 */
class BaseAppLifecyclesImpl : AppLifecycles {

    override fun attachBaseContext(base: Context) {
        MultiDex.install(base)
    }

    override fun onCreate(application: Application) {
        //适配Android P : 不同进程不可以共享同一个webView数据目录
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            val processName = CBaseUtils.getCurProcessName(application)
            KLog.i(AppConstants.LOG_TAG, "processName is $processName")
            if (!CBaseUtils.isMainProcess(application)){
                WebView.setDataDirectorySuffix(processName)
            }
        }

        if (CBaseUtils.isMainProcess(application)) {
            registerActivityLifecycle(application)

            if (AppEnv.DEV_MODE) {
                KLog.setLogPriority(KLog.V)
            } else {
                KLog.setLogPriority(KLog.I)
            }

            doAppInitial(application)
            NetworkManager.getDefault().init(application)
        }
    }

    override fun onTerminate(application: Application) {

    }

    private fun registerActivityLifecycle(application: Application) {
        AppUtils.registerAppStatusChangedListener("iFund", object : AppUtils.OnAppStatusChangedListener {

            override fun onForeground() {
                KLog.d(AppConstants.LOG_TAG, "app comes to foreground")

                BusProvider.getInstance().post(CommonBusEvent.AppComeForegroundEvent())
            }

            override fun onBackground() {
                KLog.d(AppConstants.LOG_TAG, "app comes to background")
            }
        })
    }

    private fun doAppInitial(context: Application){
        if (AppEnv.DEV_MODE) {
            ARouter.openLog()     // 打印日志
            ARouter.openDebug()   // 开启调试模式(如果在InstantRun模式下运行，必须开启调试模式！线上版本需要关闭,否则有安全风险)
        }
        ARouter.init(context)

        Realm.init(context)

        //error catch
        CustomActivityOnCrash.install(context)

        UMConfigure.setLogEnabled(AppEnv.DEV_MODE)
        // 初始化SDK
        UMConfigure.init(context, AppEnv.UMENG_APP_KEY, AppEnv.APP_FLAVOR, UMConfigure.DEVICE_TYPE_PHONE, null)
        // 选用AUTO页面采集模式
        MobclickAgent.setPageCollectionMode(MobclickAgent.PageMode.AUTO)

//        //loading and retry layout
        Gloading.debug(AppEnv.DEV_MODE)

        ZXingLibrary.initDisplayOpinion(context)
    }
}
